package cn.fl.flow.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.comm.facade.upload.dto.SysUlFilesRecDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.condition.SysOrgQueryCondition;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapOverdueInfoQueryCondition;
import cn.fl.capital.condition.CapPayInfoQueryCondition;
import cn.fl.capital.service.ICapOverdueInfoService;
import cn.fl.capital.service.ICapPayInfoService;
import cn.fl.capital.vo.CapOverdueInfoVO;
import cn.fl.capital.vo.CapPayInfoVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.flow.service.IFlowService;
import cn.fl.flow.transform.BusinessFlowTaskInfoVOTransform;
import cn.fl.flow.transform.FlowMyTaskVOTransform;
import cn.fl.flow.transform.SysFlowAgentUsrVOTransform;
import cn.fl.flow.vo.FlowTaskInfoVO;
import cn.fl.flow.vo.SysFlowAgentUsrVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjBscInfoQueryCondition;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.condition.PrjPrdPaySchMQueryCondition;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.service.IPrjPrdPaySchDService;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.vo.PrjPrdPaySchDVO;
import cn.fl.project.vo.PrjPrdPaySchMVO;
import cn.fl.system.transform.SysFlowManageVVOTransform;
import cn.fl.system.transform.SysFlowTaskHVOTransform;
import cn.fl.system.vo.SysFlowManageVVO;
import cn.fl.system.vo.SysFlowTaskHVO;
import com.lionbridge.activiti.constant.EnumInterface.TaskType;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.api.IFlowSyncDataApiService;
import fl.flow.facade.flow.condition.FlowQueryCondition;
import fl.flow.facade.flow.condition.SysFlowManageQueryCondition;
import fl.flow.facade.flow.condition.SysFlowManageVQueryCondition;
import fl.flow.facade.flow.dto.*;
import flow.business.facade.flow.api.IFlowBusinessModifyApiService;
import flow.business.facade.flow.api.IFlowBusinessQueryApiService;
import flow.business.facade.flow.transform.FlowCompleteParamsDTOTransform;
import flow.business.facade.flow.transform.SysFlowTaskHDTOTransform;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 我的任务服务接口实现
 * @author FUJINGRUN
 *
 */
@Service
public class FlowServiceImpl implements IFlowService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(FlowServiceImpl.class);

	/**
	 * 流程API接口服务
	 */
	@Autowired
	private IFlowApiService flowApiService;
	
	/**
	 * 流程同步数据API服务
	 */
	@Autowired
	private IFlowSyncDataApiService flowSyncDataApiService;
	
	/**
	 * CstBscInfo 服务层BEAN
	 */
	@Autowired
	private ICstBscInfoService cstBscInfoService;
	
	/**
	 * 上传下载服务
	 */
	@Autowired
	private IUploadApiService uploadApiService;
	
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	@Autowired
	private IProjectApiService projectApiService;
	
	@Autowired
	private ISysOrgApiService sysOrgApiService;
	
	@Autowired
	private IPrjPrdPaySchMService prjPrdPaySchMService;
	
	@Autowired
	private IPrjPrdPaySchDService prjPrdPaySchDService;
	
    @Autowired
    private ICapPayInfoService capPayInfoService;
    
    @Autowired
    private ICapOverdueInfoService capOverdueInfoService;

	@Autowired
	private IFlowBusinessQueryApiService flowBusinessQueryApiService;
	@Autowired
	private IFlowBusinessModifyApiService flowBusinessModifyApiService;


	@SuppressWarnings("unchecked")
	@Override
	public Pager searchMyTaskRecordPage(Pager page, Long userId, String status) throws BaseException{
		page = this.flowApiService.searchMyTaskRecordPage(page, userId, status);
		List<FlowMyTaskDTO> myTaskRecordDTOList = (List<FlowMyTaskDTO>)page.getResultList();
		this.setMyTaskOrgInfo(myTaskRecordDTOList);
		page.setResultList(FlowMyTaskVOTransform.toVOList(myTaskRecordDTOList));
		return page;
	}



	@Override
	public List<FlowTaskInfoVO> searchFlowTaskVOInfoList(String processDefinitionId, String processInstanceId,
			String taskId, String originTaskId, boolean nextTaskTip) throws BaseException {
		List<FlowTaskInfoVO> flowTaskInfoVOList = new ArrayList<FlowTaskInfoVO>();
		//List<FlowTaskInfoDTO> flowTaskInfoDTOList = this.flowApiService.searchFlowTaskDTOInfoList(processDefinitionId,
		//		processInstanceId, taskId, originTaskId, CurrentThreadContext.getFlServiceSysCd(), nextTaskTip);
		List<flow.business.facade.flow.dto.FlowTaskInfoDTO> flowTaskInfoDTOList = flowBusinessQueryApiService.searchFlowTaskDTOInfoList(processDefinitionId,
				processInstanceId, taskId, originTaskId, CurrentThreadContext.getFlServiceSysCd(), nextTaskTip);
		if(CollectionUtils.isNotEmpty(flowTaskInfoDTOList)){
			//flowTaskInfoVOList = FlowTaskInfoVOTransform.toVOList(flowTaskInfoDTOList);
			flowTaskInfoVOList = BusinessFlowTaskInfoVOTransform.toVOList(flowTaskInfoDTOList);
		}
		return flowTaskInfoVOList;
	}

	@Override
	public DataResultDTO modifyFlowSign(SysFlowTaskHVO entity) throws BaseException {
		SysFlowTaskHDTO sysFlowTaskHDTO = SysFlowTaskHVOTransform.toDTO(entity);
		return this.flowApiService.modifyFlowSign(sysFlowTaskHDTO);
	}

	@Override
	public Boolean modifyFlowPrivate(SysFlowTaskHVO sysFlowTaskHVO) throws BaseException {
		SysFlowTaskHDTO sysFlowTaskHDTO = SysFlowTaskHVOTransform.toDTO(sysFlowTaskHVO);
		//判断是否调用新流程处理方法
		if(getFlowBranch(sysFlowTaskHVO.getProcDefKey())){
			//调动流程dubbo服务
			return this.flowBusinessModifyApiService.modifyFlowPrivate(SysFlowTaskHDTOTransform.transformBusinessDTO(sysFlowTaskHDTO));
		}else {
			return this.flowApiService.modifyFlowPrivate(sysFlowTaskHDTO);
		}
	}
	@Override
	public DataResultDTO modifyFlowComplete(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
		//判断是否调用新流程处理方法
		if(getFlowBranch(flowCompleteParamsDTO.getProcDefKey())){
			//调动流程dubbo服务
			return this.flowBusinessModifyApiService.modifyFlowComplete(FlowCompleteParamsDTOTransform.transformBusinessDTO(flowCompleteParamsDTO));
		}else {
			//调用原有fl-flow-service
			return this.flowApiService.modifyFlowComplete(flowCompleteParamsDTO);
		}
	}


	@Override
	public DataResultDTO modifyFlowDistribute(SysFlowTaskHVO sysFlowTaskHVO) throws BaseException {
		SysFlowTaskHDTO sysFlowTaskHDTO = SysFlowTaskHVOTransform.toDTO(sysFlowTaskHVO);
		return this.flowApiService.modifyFlowDistribute(sysFlowTaskHDTO);
	}

	@Override
	public DataResultDTO modifyFlowHold(SysFlowTaskHVO sysFlowTaskHVO) throws BaseException {
		
		if(null == sysFlowTaskHVO){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "对象不能为空!");
		}
		
		Long usrId = sysFlowTaskHVO.getCrtUsrId();
		String taskId = sysFlowTaskHVO.getTaskId();
		String processInstanceId = sysFlowTaskHVO.getProcInstId();
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "当前用户不能为空!");
		}
		if(StringUtils.isEmpty(taskId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "任务ID不能为空!");
		}
		if(StringUtils.isEmpty(processInstanceId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "流程实例ID不能为空!");
		}
		SysFlowTaskHDTO dto = SysFlowTaskHVOTransform.toDTO(sysFlowTaskHVO);
		return this.flowApiService.modifyFlowHold(dto);
	}
	
	@Override
	public Boolean modifyFlowRecover(Long usrId, String taskId, String processInstanceId) throws BaseException {
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "当前用户不能为空!");
		}
		if(StringUtils.isEmpty(taskId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "任务ID不能为空!");
		}
		if(StringUtils.isEmpty(processInstanceId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "流程实例ID不能为空!");
		}
		return this.flowApiService.modifyFlowRecover(usrId, taskId, processInstanceId);
	}

	@Override
	public Boolean modifyFlowCancel(Long usrId, String taskId, String processInstanceId) throws BaseException {
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "当前用户不能为空!");
		}
		if(StringUtils.isEmpty(taskId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "任务ID不能为空!");
		}
		if(StringUtils.isEmpty(processInstanceId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "流程实例ID不能为空!");
		}
		return this.flowApiService.modifyFlowCancel(usrId, taskId, processInstanceId);
	}

	@Override
	public Boolean modifyFlowRefused(Long usrId, String taskId) throws BaseException {
		return this.flowApiService.modifyFlowRefused(usrId, taskId);
	}

	@Override
	public Boolean syncFlowDefDataFromRemote(String sysCd) throws BaseException {
		if(StringUtils.isEmpty(sysCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统代码不能为空!");
		}
		return this.flowSyncDataApiService.syncFlowDefDataFromRemote(sysCd, CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public Pager searchFlowAgentUsrListPage(Long usrId, Pager page) {
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "当前用户不能为空!");
		}
		FlowQueryCondition qc = (FlowQueryCondition)page.getCondition();
		if(StringUtils.isEmpty(qc.getSysCd())) {//系统代码必须存在
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统代码不能为空!");
		};
		return this.flowApiService.searchFlowAgentUsrListPage(usrId, page);
	}

	@Override
	public Boolean addFlowAgentUsr(SysFlowAgentUsrVO sysFlowAgentUsrVO) {
		if(StringUtils.isEmpty(sysFlowAgentUsrVO.getSysCd())) {//系统代码必须存在
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统代码不能为空!");
		};
		return this.flowApiService.addFlowAgentUsr(SysFlowAgentUsrVOTransform.toDTO(sysFlowAgentUsrVO));
	}

	@Override
	public Boolean removeFlowAgentUsr(String dataId) {
		if(StringUtils.isEmpty(dataId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "数据主键不能为空!");
		};
		return this.flowApiService.removeFlowAgentUsr(dataId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchMyTaskWJPage(Pager page, Long userId) throws BaseException {
		page = this.flowApiService.searchMyTaskWJPage(page, userId);
		List<FlowMyTaskDTO> myTaskRecordDTOList = (List<FlowMyTaskDTO>)page.getResultList();
		page.setResultList(FlowMyTaskVOTransform.toVOList(myTaskRecordDTOList));
		return page;
	}

	@Override
	public List<KeyValueVO> searchBusinessSourceList() throws BaseException {
		List<KeyValueVO> list = new ArrayList<>();
		KeyValueVO kv = null;
		for (TaskType taskType : TaskType.values()) {
			kv = new KeyValueVO();
			kv.setKey(taskType.key());
			kv.setValue(TaskType.valueOf(taskType.key()).desc());
			list.add(kv);
		}
		return list;
	}

	@Override
	public List<KeyValueVO> searchBusinessOrgList() throws BaseException {
		return this.cstBscInfoService.searchBusinessOrgList();
	}

	@Override
	public List<KeyValueVO> searchBusinessTypeList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_SYS_BUSINESS_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public List<KeyValueVO> searchFlowAllowMe(Long usrId, Long orgId, String sysCd, int isAuth) throws BaseException {
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户主键不能为空!");
		}
		if(StringUtils.isEmpty(orgId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "组织机构ID不能为空!");
		}
		if(StringUtils.isEmpty(sysCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统代码不能为空!");
		}
		List<KeyValueVO> list = new ArrayList<>();
		try {
			List<SysFlowManageDTO> sysFlowManageDTOList = null;
			Object orgTypCd = CurrentThreadContext.getValue(CurrentThreadContext.CURRENT_ORG_TYP_CD);
			if(null == orgTypCd){
				orgTypCd = "";
			}
			//加上一个判断,如果是非事业部的则限制所有的流程定义信息
			if(isAuth == 0 || StringUtils.isEmpty(orgTypCd.toString())){
				SysFlowManageQueryCondition sysFlowManageQueryCondition = new SysFlowManageQueryCondition();
				sysFlowManageQueryCondition.setSysCd(sysCd);
				sysFlowManageDTOList = flowApiService.searchFlowManageList(sysFlowManageQueryCondition);
			}else{
				sysFlowManageDTOList = this.flowApiService.searchFlowManageListAllowMe(usrId, orgId, sysCd);
			}
			if(CollectionUtils.isNotEmpty(sysFlowManageDTOList)){
				KeyValueVO keyValueVO = null;
				for (SysFlowManageDTO sysFlowManageDTO : sysFlowManageDTOList) {
					keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysFlowManageDTO.getProcDefKey());
					keyValueVO.setValue(sysFlowManageDTO.getProcDefName());
					list.add(keyValueVO);
				}
			}
		}catch (BaseException e) {
			logger.error("IFlowService.searchFlowAllowMe BaseException获取我可以设置的流程数据失败！"+e.getMessage());
		}catch (Exception e) {
			logger.error("IFlowService.searchFlowAllowMe Exception获取我可以设置的流程数据失败！"+e.getMessage());
		}
		
		return list;
	}

	@Override
	public List<KeyValueVO> searchModelTypList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(fl.flow.facade.flow.framework.constant.DictionaryConstant.CODE_GRP_FLOW_MODEL_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public Boolean addFlowAttchmnt(Long id, String attchmnt, FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
		Boolean pass = false;
		if(StringUtils.isEmpty(id) || StringUtils.isEmpty(attchmnt)){
			return pass;
		}
		try {
			JSONArray array = JSONArray.fromObject(attchmnt);
			if(null != array && !array.isEmpty() && array.isArray()){//处理附件
				String pInstId = flowCompleteParamsDTO.getProcInstId();
				String tInstId = flowCompleteParamsDTO.getTaskId();
				List<SysUlFilesRecDTO> addList = new ArrayList<>();//需要新加的附件内容
				SysUlFilesRecDTO sysUlFilesRecDTO = null;//
				for (Object object : array) {
					JSONObject upObj = JSONObject.fromObject(object);
					String flPath = upObj.optString(SysUlFilesRecConstant.REQUEST_PARAM_FILE_PATH, SysUlFilesRecConstant.REQUEST_PARAM_DEFAULT_UNKNOW);
					String flNm = upObj.optString(SysUlFilesRecConstant.REQUEST_PARAM_FILE_NAME, SysUlFilesRecConstant.REQUEST_PARAM_DEFAULT_UNKNOW);
					sysUlFilesRecDTO = new SysUlFilesRecDTO();
					sysUlFilesRecDTO.setCrtTm(new Date());
					sysUlFilesRecDTO.setTabNm(SysUlFilesRecConstant.TB_NAME_SYS_FLOW_TASK_H);
					sysUlFilesRecDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					sysUlFilesRecDTO.setPkId(String.valueOf(id));
					sysUlFilesRecDTO.setFileNm(flNm);
					sysUlFilesRecDTO.setFilePath(flPath);
					sysUlFilesRecDTO.setIsDel(BaseConstant.IS_YESNO_NO);
					sysUlFilesRecDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
					sysUlFilesRecDTO.setTransFlg(BaseConstant.IS_YESNO_NO);
					sysUlFilesRecDTO.setPInstId(pInstId);
					sysUlFilesRecDTO.setTInstId(tInstId);
					addList.add(sysUlFilesRecDTO);
				}
				this.uploadApiService.addSysUlFilesRecList(addList);
			}
		}catch (JSONException e) {
			BaseLogUtils.info(logger, "addFlowAttchmnt", "上传文件失败!" + e.getMessage());
		}catch (Exception e) {
			BaseLogUtils.error(logger, "addFlowAttchmnt", "上传失败!" + e.getMessage());
		}
		return pass;
	}

	@Override
	public List<KeyValueVO> searchRejRsnList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_RSN_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public List<SysFlowManageVVO> searchFlowManageVListForStart(SysFlowManageVQueryCondition condition) throws BaseException {
		if(null == condition){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询对象不能为空!");
		}
		if(StringUtils.isEmpty(condition.getSysCd())){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统代码不能为空!");
		}
		
		if(StringUtils.isEmpty(condition.getOrgCd())){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "组织架构代码不能为空!");
		}
		
		if(StringUtils.isEmpty(condition.getModelTyp())){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "模块类型不能为空!");
		}
		List<SysFlowManageVDTO> DTOList = this.flowApiService.searchFlowManageVListForStart(condition);
		return SysFlowManageVVOTransform.toVOList(DTOList);
	}

	@Override
	public List<SysUsrFunAuthRDTO> searchAgentUsrByRole(Long roleId, String processDefKey, String taskDefKey,
														Long businessKey, String beanName, String sysCd) throws BaseException {
		//判断是否调用新dubbo流程服务
		if (this.getFlowBranch(processDefKey)) {
			//调用新dubbo流程服务
			return this.flowBusinessQueryApiService.searchAgentUsrByRole(roleId, processDefKey, taskDefKey, businessKey, beanName, sysCd,null);
		} else {
			//调用原有流程服务
			return this.flowApiService.searchAgentUsrByRole(roleId, processDefKey, taskDefKey, businessKey, beanName, sysCd);
		}
	}


	@Override
	public String modifyFlowBack(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
//		return this.flowApiService.modifyFlowBack(flowCompleteParamsDTO);
        //判断是否调用新dubbo流程服务
        if (this.getFlowBranch(flowCompleteParamsDTO.getProcDefKey())) {
            //调用新dubbo流程服务
            return this.flowBusinessModifyApiService.modifyFlowBack(FlowCompleteParamsDTOTransform.transformBusinessDTO(flowCompleteParamsDTO));
        } else {
            //调用原有流程服务
            return this.flowApiService.modifyFlowBack(flowCompleteParamsDTO);
        }
	}

	@Override
	@SuppressWarnings("unchecked")
	public Pager searchTaskRecordManagePage(Pager page, Long userId, String status) throws BaseException {
		page = this.flowApiService.searchTaskRecordManagePage(page, userId, status);
		List<FlowMyTaskDTO> myTaskRecordDTOList = (List<FlowMyTaskDTO>)page.getResultList();
		this.setMyTaskOrgInfo(myTaskRecordDTOList);
		page.setResultList(FlowMyTaskVOTransform.toVOList(myTaskRecordDTOList));
		return page;
	}

	@Override
	public Boolean modifyAssignTask(String taskId, List<String> usrIdList, String originalTaskId) throws BaseException {
		if(StringUtils.isEmpty(taskId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "任务ID不能为空!");
		}
		
		if(CollectionUtils.isEmpty(usrIdList)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "处理人不能为空!");
		}
		return this.flowApiService.modifyAssignTask(taskId, usrIdList, originalTaskId);
	}

	@Override
	public Boolean modifyInitiator(String processInstanceId, String usrId, String usrName) throws BaseException {
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "发起ID不能为空!");
		}
		return this.flowApiService.modifyInitiator(processInstanceId, usrId, usrName);
	}
	
	@Override
	public DataResultDTO modifyRobotFlowBack(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
//		return this.flowApiService.modifyRobotFlowBack(flowCompleteParamsDTO);
        if (this.getFlowBranch(flowCompleteParamsDTO.getProcDefKey())) {
            //调用新dubbo流程服务
            return this.flowBusinessModifyApiService.modifyRobotFlowBack(FlowCompleteParamsDTOTransform.transformBusinessDTO(flowCompleteParamsDTO));
        } else {
            //调用原有流程服务
            return this.flowApiService.modifyRobotFlowBack(flowCompleteParamsDTO);
        }
	}

	//设置我的任务列表中组织架构信息
	private void setBusinessMyTaskOrgInfo(List<flow.business.facade.flow.dto.FlowMyTaskDTO> myTaskRecordDTOList) {
		// 处理分公司
		if (CollectionUtils.isNotEmpty(myTaskRecordDTOList)) {
			List<Long> prjIds = new ArrayList<>();
			List<Long> payInfoIds = new ArrayList<>();
			List<Long> prjPaySchDIds = new ArrayList<>();
			List<Long> capOverdueIds = new ArrayList<>();
			for (flow.business.facade.flow.dto.FlowMyTaskDTO flowMyTaskDTO : myTaskRecordDTOList) {
				String id = flowMyTaskDTO.getBusinessKey();// 过滤出来项目的ID,然后根据项目ID获取客户经理所在分公司组织架构代码,在拿组织架构获取名称
				// 170103077000018 获取789位的 077是属于项目的
				String isSubId = "";
				try {
					isSubId = id.substring(6, 9);
				} catch (Exception e) {
					continue;
				}
				Long key = Long.valueOf(id);
				if (isSubId.equals("077")) {// 获取属于PRJ_BSC_INFO 项目立项流程
					prjIds.add(key);
				} else if (isSubId.equals("020")) {// 获取属于CAP_PAY_INFO 放款流程
					payInfoIds.add(key);
				} else if (isSubId.equals("103")) {// 获取属于支付表明细的
					// PRJ_PRD_PAY_SCH_D 风险保证金
					prjPaySchDIds.add(key);
				} else if (isSubId.equals("017")) {// 获取属于CAP_OVERDUE_INFO
					// 罚息减免流程
					capOverdueIds.add(key);
				}
			}

			Map<Long, Long> bussKeyAndPayInfoKeyMap = new HashMap<>();
			try {
				if (CollectionUtils.isNotEmpty(payInfoIds)) {// 通过支付表获取项目ID
					CapPayInfoQueryCondition capPayInfoQueryCondition = new CapPayInfoQueryCondition();
					capPayInfoQueryCondition.setIdList(payInfoIds);
					List<CapPayInfoVO> capPayInfoVOList = this.capPayInfoService.searchList(capPayInfoQueryCondition);
					if (CollectionUtils.isNotEmpty(capPayInfoVOList)) {
						for (CapPayInfoVO capPayInfoVO : capPayInfoVOList) {
							Long prjId = capPayInfoVO.getPrjId();
							if (StringUtils.isNotEmpty(prjId)) {
								prjIds.add(prjId);
								bussKeyAndPayInfoKeyMap.put(capPayInfoVO.getId(), prjId);
							}
						}
					}
				}
			} catch (Exception e) {
				BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取CapPayInfoVO集合失败!");
			}

			Map<Long, Long> bussKeyAndprjPaySchDIdKeyMap = new HashMap<>();
			try {
				if (CollectionUtils.isNotEmpty(prjPaySchDIds)) {
					PrjPrdPaySchDQueryCondition prjPrdPaySchDQueryCondition = new PrjPrdPaySchDQueryCondition();
					prjPrdPaySchDQueryCondition.setIdList(prjPaySchDIds);
					List<PrjPrdPaySchDVO> prjPrdPaySchDList = this.prjPrdPaySchDService
							.searchList(prjPrdPaySchDQueryCondition);
					if (CollectionUtils.isNotEmpty(prjPrdPaySchDList)) {
						List<Long> paySchMIdList = new ArrayList<>();
						for (PrjPrdPaySchDVO prjPrdPaySchDVO : prjPrdPaySchDList) {
							Long paySchMId = prjPrdPaySchDVO.getPaySchId();
							if (StringUtils.isNotEmpty(paySchMId)) {
								paySchMIdList.add(paySchMId);
							}
						}

						if (CollectionUtils.isNotEmpty(paySchMIdList)) {
							PrjPrdPaySchMQueryCondition prjPrdPaySchMQueryCondition = new PrjPrdPaySchMQueryCondition();
							prjPrdPaySchMQueryCondition.setIdList(paySchMIdList);
							List<PrjPrdPaySchMVO> prjPrdPaySchMList = this.prjPrdPaySchMService
									.searchList(prjPrdPaySchMQueryCondition);
							if (CollectionUtils.isNotEmpty(prjPrdPaySchMList)) {
								for (PrjPrdPaySchMVO prjPrdPaySchMVO : prjPrdPaySchMList) {
									Long prjId = prjPrdPaySchMVO.getPrjId();
									if (StringUtils.isNotEmpty(prjId)) {
										prjIds.add(prjId);
										bussKeyAndprjPaySchDIdKeyMap.put(prjPrdPaySchMVO.getId(), prjId);
									}
								}
							}
						}
					}
				}
			} catch (Exception e) {
				BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取PrjPrdPaySchDVO集合失败!");
			}

			Map<Long, Long> bussKeyAndcapOverdueIdKeyMap = new HashMap<>();
			try {
				if (CollectionUtils.isNotEmpty(capOverdueIds)) {// 通过支付表获取项目ID
					CapOverdueInfoQueryCondition capOverdueInfoQueryCondition = new CapOverdueInfoQueryCondition();
					capOverdueInfoQueryCondition.setIdList(payInfoIds);
					List<CapOverdueInfoVO> capOverdueInfoVOList = this.capOverdueInfoService
							.searchList(capOverdueInfoQueryCondition);
					if (CollectionUtils.isNotEmpty(capOverdueInfoVOList)) {
						List<Long> paySchMIdList = new ArrayList<>();
						for (CapOverdueInfoVO capOverdueInfoVO : capOverdueInfoVOList) {
							Long paySchMId = capOverdueInfoVO.getPaySchId();
							if (StringUtils.isNotEmpty(paySchMId)) {
								paySchMIdList.add(paySchMId);
							}
						}

						if (CollectionUtils.isNotEmpty(paySchMIdList)) {
							PrjPrdPaySchMQueryCondition prjPrdPaySchMQueryCondition = new PrjPrdPaySchMQueryCondition();
							prjPrdPaySchMQueryCondition.setIdList(paySchMIdList);
							List<PrjPrdPaySchMVO> prjPrdPaySchMList = this.prjPrdPaySchMService
									.searchList(prjPrdPaySchMQueryCondition);
							if (CollectionUtils.isNotEmpty(prjPrdPaySchMList)) {
								for (PrjPrdPaySchMVO prjPrdPaySchMVO : prjPrdPaySchMList) {
									Long prjId = prjPrdPaySchMVO.getPrjId();
									if (StringUtils.isNotEmpty(prjId)) {
										prjIds.add(prjId);
										bussKeyAndcapOverdueIdKeyMap.put(prjPrdPaySchMVO.getId(), prjId);
									}
								}
							}
						}
					}
				}
			} catch (Exception e) {
				BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取CapOverdueInfoVO集合失败!");
			}

			try {
				if (CollectionUtils.isNotEmpty(prjIds)) {
					PrjBscInfoQueryCondition prjBscInfoQueryCondition = new PrjBscInfoQueryCondition();
					prjBscInfoQueryCondition.setIdList(prjIds);
					List<PrjBscInfoDTO> prjList = this.projectApiService.searchPrjBscInfoList(prjBscInfoQueryCondition);
					Map<Long, String> prjMap = new HashMap<>();
					List<String> orgCdList = new ArrayList<>();
					if (CollectionUtils.isNotEmpty(prjList)) {
						for (PrjBscInfoDTO prjBscInfoDTO : prjList) {
							String cstMgrOrgCd = prjBscInfoDTO.getCstMgrOrgCd();
							if (StringUtils.isEmpty(cstMgrOrgCd)) {
								continue;
							}
							orgCdList.add(cstMgrOrgCd);
							prjMap.put(prjBscInfoDTO.getId(), cstMgrOrgCd);
						}
					}

					if (CollectionUtils.isNotEmpty(orgCdList)) {
						SysOrgQueryCondition sysOrgQueryCondition = new SysOrgQueryCondition();
						sysOrgQueryCondition.setOrgCdList(orgCdList);
						sysOrgQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
						List<SysOrgDTO> sysOrgDTOList = this.sysOrgApiService.searchSysOrgList(sysOrgQueryCondition);
						Map<String, String> orgCdWithNmMap = new HashMap<>();
						if (CollectionUtils.isNotEmpty(sysOrgDTOList)) {
							for (SysOrgDTO sysOrgDTO : sysOrgDTOList) {
								orgCdWithNmMap.put(sysOrgDTO.getOrgCd(), sysOrgDTO.getOrgShtNm());
							}
						}

						if (CollectionUtils.isMapNotEmpty(orgCdWithNmMap)) {
							for (flow.business.facade.flow.dto.FlowMyTaskDTO flowMyTaskDTO : myTaskRecordDTOList) {
								Long id = Long.valueOf(flowMyTaskDTO.getBusinessKey());
								if (bussKeyAndPayInfoKeyMap.containsKey(id)) {
									id = bussKeyAndPayInfoKeyMap.get(id);
								} else if (bussKeyAndprjPaySchDIdKeyMap.containsKey(id)) {
									id = bussKeyAndprjPaySchDIdKeyMap.get(id);
								}

								String orgCd = prjMap.get(id);
								if (orgCdWithNmMap.containsKey(orgCd)) {
									flowMyTaskDTO.setOrgNm(orgCdWithNmMap.get(orgCd));
								}
							}
						}

						orgCdWithNmMap.clear();
						orgCdWithNmMap = null;
					}

				}
			} catch (Exception e) {
				BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取组织架构信息赋值失败!");
			}

			bussKeyAndPayInfoKeyMap.clear();
			bussKeyAndPayInfoKeyMap = null;

			bussKeyAndprjPaySchDIdKeyMap.clear();
			bussKeyAndprjPaySchDIdKeyMap = null;

			bussKeyAndcapOverdueIdKeyMap.clear();
			bussKeyAndcapOverdueIdKeyMap = null;
		}
	}
	
	//设置我的任务列表中组织架构信息
	private void setMyTaskOrgInfo(List<FlowMyTaskDTO> myTaskRecordDTOList) {
		// 处理分公司
		if (CollectionUtils.isNotEmpty(myTaskRecordDTOList)) {
			List<Long> prjIds = new ArrayList<>();
			List<Long> payInfoIds = new ArrayList<>();
			List<Long> prjPaySchDIds = new ArrayList<>();
			List<Long> capOverdueIds = new ArrayList<>();
			for (FlowMyTaskDTO flowMyTaskDTO : myTaskRecordDTOList) {
				String id = flowMyTaskDTO.getBusinessKey();// 过滤出来项目的ID,然后根据项目ID获取客户经理所在分公司组织架构代码,在拿组织架构获取名称
				// 170103077000018 获取789位的 077是属于项目的
				String isSubId = "";
				try {
					isSubId = id.substring(6, 9);
				} catch (Exception e) {
					continue;
				}
				Long key = Long.valueOf(id);
				if (isSubId.equals("077")) {// 获取属于PRJ_BSC_INFO 项目立项流程
					prjIds.add(key);
				} else if (isSubId.equals("020")) {// 获取属于CAP_PAY_INFO 放款流程
					payInfoIds.add(key);
				} else if (isSubId.equals("103")) {// 获取属于支付表明细的
													// PRJ_PRD_PAY_SCH_D 风险保证金
					prjPaySchDIds.add(key);
				} else if (isSubId.equals("017")) {// 获取属于CAP_OVERDUE_INFO
													// 罚息减免流程
					capOverdueIds.add(key);
				}
			}

			Map<Long, Long> bussKeyAndPayInfoKeyMap = new HashMap<>();
			try {
				if (CollectionUtils.isNotEmpty(payInfoIds)) {// 通过支付表获取项目ID
					CapPayInfoQueryCondition capPayInfoQueryCondition = new CapPayInfoQueryCondition();
					capPayInfoQueryCondition.setIdList(payInfoIds);
					List<CapPayInfoVO> capPayInfoVOList = this.capPayInfoService.searchList(capPayInfoQueryCondition);
					if (CollectionUtils.isNotEmpty(capPayInfoVOList)) {
						for (CapPayInfoVO capPayInfoVO : capPayInfoVOList) {
							Long prjId = capPayInfoVO.getPrjId();
							if (StringUtils.isNotEmpty(prjId)) {
								prjIds.add(prjId);
								bussKeyAndPayInfoKeyMap.put(capPayInfoVO.getId(), prjId);
							}
						}
					}
				}
			} catch (Exception e) {
			BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取CapPayInfoVO集合失败!");
			}

			Map<Long, Long> bussKeyAndprjPaySchDIdKeyMap = new HashMap<>();
			try {
				if (CollectionUtils.isNotEmpty(prjPaySchDIds)) {
					PrjPrdPaySchDQueryCondition prjPrdPaySchDQueryCondition = new PrjPrdPaySchDQueryCondition();
					prjPrdPaySchDQueryCondition.setIdList(prjPaySchDIds);
					List<PrjPrdPaySchDVO> prjPrdPaySchDList = this.prjPrdPaySchDService
							.searchList(prjPrdPaySchDQueryCondition);
					if (CollectionUtils.isNotEmpty(prjPrdPaySchDList)) {
						List<Long> paySchMIdList = new ArrayList<>();
						for (PrjPrdPaySchDVO prjPrdPaySchDVO : prjPrdPaySchDList) {
							Long paySchMId = prjPrdPaySchDVO.getPaySchId();
							if (StringUtils.isNotEmpty(paySchMId)) {
								paySchMIdList.add(paySchMId);
							}
						}

						if (CollectionUtils.isNotEmpty(paySchMIdList)) {
							PrjPrdPaySchMQueryCondition prjPrdPaySchMQueryCondition = new PrjPrdPaySchMQueryCondition();
							prjPrdPaySchMQueryCondition.setIdList(paySchMIdList);
							List<PrjPrdPaySchMVO> prjPrdPaySchMList = this.prjPrdPaySchMService
									.searchList(prjPrdPaySchMQueryCondition);
							if (CollectionUtils.isNotEmpty(prjPrdPaySchMList)) {
								for (PrjPrdPaySchMVO prjPrdPaySchMVO : prjPrdPaySchMList) {
									Long prjId = prjPrdPaySchMVO.getPrjId();
									if (StringUtils.isNotEmpty(prjId)) {
										prjIds.add(prjId);
										bussKeyAndprjPaySchDIdKeyMap.put(prjPrdPaySchMVO.getId(), prjId);
									}
								}
							}
						}
					}
				}
			} catch (Exception e) {
			BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取PrjPrdPaySchDVO集合失败!");
			}

			Map<Long, Long> bussKeyAndcapOverdueIdKeyMap = new HashMap<>();
			try {
				if (CollectionUtils.isNotEmpty(capOverdueIds)) {// 通过支付表获取项目ID
					CapOverdueInfoQueryCondition capOverdueInfoQueryCondition = new CapOverdueInfoQueryCondition();
					capOverdueInfoQueryCondition.setIdList(payInfoIds);
					List<CapOverdueInfoVO> capOverdueInfoVOList = this.capOverdueInfoService
							.searchList(capOverdueInfoQueryCondition);
					if (CollectionUtils.isNotEmpty(capOverdueInfoVOList)) {
						List<Long> paySchMIdList = new ArrayList<>();
						for (CapOverdueInfoVO capOverdueInfoVO : capOverdueInfoVOList) {
							Long paySchMId = capOverdueInfoVO.getPaySchId();
							if (StringUtils.isNotEmpty(paySchMId)) {
								paySchMIdList.add(paySchMId);
							}
						}

						if (CollectionUtils.isNotEmpty(paySchMIdList)) {
							PrjPrdPaySchMQueryCondition prjPrdPaySchMQueryCondition = new PrjPrdPaySchMQueryCondition();
							prjPrdPaySchMQueryCondition.setIdList(paySchMIdList);
							List<PrjPrdPaySchMVO> prjPrdPaySchMList = this.prjPrdPaySchMService
									.searchList(prjPrdPaySchMQueryCondition);
							if (CollectionUtils.isNotEmpty(prjPrdPaySchMList)) {
								for (PrjPrdPaySchMVO prjPrdPaySchMVO : prjPrdPaySchMList) {
									Long prjId = prjPrdPaySchMVO.getPrjId();
									if (StringUtils.isNotEmpty(prjId)) {
										prjIds.add(prjId);
										bussKeyAndcapOverdueIdKeyMap.put(prjPrdPaySchMVO.getId(), prjId);
									}
								}
							}
						}
					}
				}
			} catch (Exception e) {
			BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取CapOverdueInfoVO集合失败!");
			}

			try {
				if (CollectionUtils.isNotEmpty(prjIds)) {
					PrjBscInfoQueryCondition prjBscInfoQueryCondition = new PrjBscInfoQueryCondition();
					prjBscInfoQueryCondition.setIdList(prjIds);
					List<PrjBscInfoDTO> prjList = this.projectApiService.searchPrjBscInfoList(prjBscInfoQueryCondition);
					Map<Long, String> prjMap = new HashMap<>();
					List<String> orgCdList = new ArrayList<>();
					if (CollectionUtils.isNotEmpty(prjList)) {
						for (PrjBscInfoDTO prjBscInfoDTO : prjList) {
							String cstMgrOrgCd = prjBscInfoDTO.getCstMgrOrgCd();
							if (StringUtils.isEmpty(cstMgrOrgCd)) {
								continue;
							}
							orgCdList.add(cstMgrOrgCd);
							prjMap.put(prjBscInfoDTO.getId(), cstMgrOrgCd);
						}
					}

					if (CollectionUtils.isNotEmpty(orgCdList)) {
						SysOrgQueryCondition sysOrgQueryCondition = new SysOrgQueryCondition();
						sysOrgQueryCondition.setOrgCdList(orgCdList);
						sysOrgQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
						List<SysOrgDTO> sysOrgDTOList = this.sysOrgApiService.searchSysOrgList(sysOrgQueryCondition);
						Map<String, String> orgCdWithNmMap = new HashMap<>();
						if (CollectionUtils.isNotEmpty(sysOrgDTOList)) {
							for (SysOrgDTO sysOrgDTO : sysOrgDTOList) {
								orgCdWithNmMap.put(sysOrgDTO.getOrgCd(), sysOrgDTO.getOrgShtNm());
							}
						}

						if (CollectionUtils.isMapNotEmpty(orgCdWithNmMap)) {
							for (FlowMyTaskDTO flowMyTaskDTO : myTaskRecordDTOList) {
								Long id = Long.valueOf(flowMyTaskDTO.getBusinessKey());
								if (bussKeyAndPayInfoKeyMap.containsKey(id)) {
									id = bussKeyAndPayInfoKeyMap.get(id);
								} else if (bussKeyAndprjPaySchDIdKeyMap.containsKey(id)) {
									id = bussKeyAndprjPaySchDIdKeyMap.get(id);
								}

								String orgCd = prjMap.get(id);
								if (orgCdWithNmMap.containsKey(orgCd)) {
									flowMyTaskDTO.setOrgNm(orgCdWithNmMap.get(orgCd));
								}
							}
						}
						
						orgCdWithNmMap.clear();
						orgCdWithNmMap = null;
					}

				}
			} catch (Exception e) {
			BaseLogUtils.error(logger, "-", "异常", e);
				BaseLogUtils.error(logger, "setMyTaskOrgInfo", "待办任务获取组织架构信息赋值失败!");
			}
			
			bussKeyAndPayInfoKeyMap.clear();
			bussKeyAndPayInfoKeyMap = null;
				
			bussKeyAndprjPaySchDIdKeyMap.clear();
			bussKeyAndprjPaySchDIdKeyMap = null;
				
			bussKeyAndcapOverdueIdKeyMap.clear();
			bussKeyAndcapOverdueIdKeyMap = null;
		}
	}

	@Override
	public DataResultDTO batchModifyInitiator(List<FlowProcessDTO> processes)
			throws BaseException {
		return this.flowApiService.batchModifyInitiator(processes);
	}

	/**
	 * 获取下一节点操作人
	 * @param key
	 * @return
	 * @throws BaseException
	 */
	@Override
	public String getNextFlowAssignee(String key) throws BaseException{
		return this.flowBusinessQueryApiService.getNextFlowUsrNm(key,CurrentThreadContext.getCurrentSysCd());
	}

	@Override
	public Boolean batchModifyAssignTask(List<String> taskIds, String usrId, String rlName) throws BaseException {
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "处理人不能为空!");
		}

		if(CollectionUtils.isEmpty(taskIds)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "任务ID不能为空!");
		}
		return this.flowBusinessModifyApiService.batchModifyAssignTask(taskIds, usrId, rlName);
	}

	@Override
	public Boolean batchModifyInitiator(List<String> processInstanceIds, String usrId, String rlName) throws BaseException {
		if(StringUtils.isEmpty(usrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "发起ID不能为空!");
		}

		if(CollectionUtils.isEmpty(processInstanceIds)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "任务ID不能为空!");
		}
		return this.flowBusinessModifyApiService.batchModifyInitiator(processInstanceIds, usrId, rlName);
	}

	/**
	 *
	 * <p>描述:通过流程key,判断流程是否走新流程，默认返回false 调用老流程<p>
	 * <p>@param
	 * <p>@return
	 * <p>@throws <p>
	 * <p>创建人：王蓬涛<p>
	 * <p>创建时间: 2019/1/16 12:05</p>
	 * <p>============================================</p>
	 */
	private Boolean getFlowBranch(String flowKey) throws BaseException{
		BaseLogUtils.info(logger, "getFlowBranch", "获取流程走向分支处理开始!");
		Boolean rtn=false;
		try{
			rtn =this.flowBusinessQueryApiService.searchFlowProcess(flowKey);
		}catch (Exception e){
			BaseLogUtils.info(logger, "getFlowBranch", "获取流程走向分支处理异常"+e.getMessage(),e);
		}
		BaseLogUtils.info(logger, "getFlowBranch", "获取流程走向分支处理结束，返回结果:"+rtn);
		return rtn;
	}
}
